"""
multiprocessing 创建两个进程，同时复制一个文件的上下两半部分，
各自复制到一个新的文件里
"""
from multiprocessing import Process, Array


def copy_file(shm, des_path, flag):
    with open(file=des_path, mode='wb') as file:
        print(shm.value)
        if flag == 0:
            file.write(shm.value[:len(shm.value) // 2])
        else:
            file.write(shm.value[len(shm.value) // 2:-1])


if __name__ == "__main__":

    jobs = []
    with open('笔记.txt', mode='rb') as file:
        file_data = file.read()
        shm = Array('c', file_data)
        p1 = Process(target=copy_file, args=(shm, '笔记1.txt', 0))
        p2 = Process(target=copy_file, args=(shm, '笔记2.txt', 1))
        jobs.append(p1)
        jobs.append(p2)
        p1.start()
        p2.start()

    for job in jobs:
        job.join()
